﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SerialTool
{
    public class Frame
    {
        /*
         * 数据格式 : 帧头 |       数据长度          |
         *                10 02 | 2Byte 高字节在前  | 
         */
        private byte[] data;

        public Frame(byte[] frame, int length)
        {
            if (frame == null) return;
            if (length <= 0) return;
            if (length > frame.Length) return;

            data = new byte[length];
            Array.Copy(frame, data, length);
        }

        /// <summary>
        /// 源端口号
        /// </summary>
        public byte SrcPort
        {
            get
            {
                return data[4];
            }
        }

        /// <summary>
        /// 目的端口号
        /// </summary>
        public byte DestPort
        {
            get
            {
                int SrcAddrLen = data[5];
                return data[6 + SrcAddrLen];
            }
        }

        /// <summary>
        /// 事务
        /// </summary>
        public byte Transaction
        {
            get
            {
                int SrcAddrLen = data[5];
                int DstAddrLen = data[7 + SrcAddrLen];

                return data[8 + SrcAddrLen + DstAddrLen];
            }
        }

        /// <summary>
        /// 命令
        /// </summary>
        public byte Cmd
        {
            get
            {
                int SrcAddrLen = data[5];
                int DstAddrLen = data[7 + SrcAddrLen];

                return data[9 + SrcAddrLen + DstAddrLen];
            }
        }


        public byte[] RawData
        {
            get { return data; }
        }

        public string ToString()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sb.Append(data[i].ToString("X2") + " ");
            }
            return sb.ToString();
        }



    }
}
